"""
    Web Service API base class
    
    @author: Jean-Lou Dupont
    
"""
import logging
import pickle

import libs.cache as cache

class WebAPI(object):
    """
        Serves as base class. Provides:
        - authentication
    """
    
    def __init_(self):
        self.user = None
        self.password = None
    
#===========================================================
#===========================================================
class StatusError(Error):
    pass

class Status(Object):
    """
        Tracks when the Access Point was last
        queried in order to follow throttling constraints
        and help, of course, help performance
    """
    
    defaultTimeout = 600 #in minutes
    
    def __init__(self, service, user, entity):
        self.user        = user
        self.service     = service
        self.entity      = entity
        self.lastFetched = None
        self.lastUpdated = None

    @classmethod
    def load( service, user, entity ):
        """
            Loads a Status Object from the cache
            
            @param service
            @param user
            @param entity
            @return: Status object or None
        """
        if (service is None):
            raise StatusError('service can\'t be None')
        if (user is None):
            raise StatusError('user can\'t be None')
        if (entity is None):
            raise StatusError('entity can\'t be None')
        
        return cache.loadFromCache(service, user, entity)
        
    def save(self, timeout = Status.defaultTimeout):
        """
            Saves the Status object in the cache
        """
        if (self.service is None):
            raise StatusError('service can\'t be None')
        if (self.user is None):
            raise StatusError('user can\'t be None')
        if (self.entity is None):
            raise StatusError('entity can\'t be None')
    
        return cache.saveInCache(self,timeout, self.service, self.user, self.entity)
    
    @staticmethod
    def _log(method, service, user, entity, result):
        logging.info( "Status.%s: service[%s] user[%s] entity[%s] result[%i]" % ( method, service, user, entity, result ) )
        
        